package com.block.datapush.special.service.infoType;

import cn.hutool.core.util.ObjectUtil;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushActionTypeEnum;
import com.block.datapush.model.dto.TaskInfoDTO;
import com.block.datapush.model.result.ApiPushResult;
import com.block.datapush.special.service.IDataInfoCommonService;
import com.block.datapush.special.service.ISpecialCostomInfoProfitService;
import com.block.datapush.special.service.ISpecialCostomUserInfoService;
import com.block.datapush.special.service.ISpecialDataInfoService;
import com.block.db.common.result.CostomUserInfoResult;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.MemberPushOrderInfo;
import com.block.db.entity.SpecialCostomInfo;
import com.block.db.entity.SpecialCostomItemInfo;
import com.block.db.mapper.SpecialDataInfoMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-15
 */
@Service
public class MerchantCrmInfoServiceImpl implements  IDataInfoCommonService {

    private static Logger log = LogFactory.getPushLog();
    @Autowired
    ISpecialCostomUserInfoService specialCostomUserInfoService;
    @Autowired
    ISpecialCostomInfoProfitService specialCostomInfoProfitService;
    @Resource
    SpecialDataInfoMapper specialDataInfoMapper;
    @Autowired
    ISpecialDataInfoService specialDataInfoService;


    /**
     * 检查是否可以推送给机构
     *
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public ApiPushResult checkUser(SpecialCostomInfo costomInfo, SpecialCostomItemInfo itemInfo, MemberExtInfo memberExtInfo) {

        ApiPushResult apiPushResult = new ApiPushResult();
        apiPushResult.setSuccess(false);
        Long number = specialDataInfoMapper.getEntityByInfoIdAndUid(itemInfo.getInfoId(), memberExtInfo.getUid());
        if (number > 0){
            return apiPushResult;
        }
        apiPushResult.setSuccess(true);
        return apiPushResult;
    }

    @Override
    public ApiPushResult registerUser(SpecialCostomInfo costomInfo, MemberPushOrderInfo pushOrderInfo, MemberExtInfo memberExtInfo) throws Exception {

        ApiPushResult apiPushResult = new ApiPushResult();
        apiPushResult.setSuccess(false);

        Long number = specialDataInfoMapper.getEntityByInfoIdAndUid(pushOrderInfo.getInfoId(), memberExtInfo.getUid());
        if (number > 0){
            apiPushResult.setMessage("该数据已经推送过了");
            return apiPushResult;
        }

        CostomUserInfoResult userInfoResult = specialCostomUserInfoService.findOneCanPushUserByCostomId(pushOrderInfo.getInfoId());
        log.info("【机构推送】可推送子账号信息【userInfoResult】{}【itemId】{}【costomId】{}",
                ObjectUtil.isNotEmpty(userInfoResult), pushOrderInfo.getTaskId(), pushOrderInfo.getInfoId());

        TaskInfoDTO taskInfoDTO = new TaskInfoDTO();
        taskInfoDTO.setInfoId(pushOrderInfo.getInfoId());
        taskInfoDTO.setInfoType(pushOrderInfo.getInfoType());
        taskInfoDTO.setInfoName(pushOrderInfo.getInfoName());
        taskInfoDTO.setTaskId(pushOrderInfo.getTaskId());
        taskInfoDTO.setPriceAmount(pushOrderInfo.getPriceAmount());
        taskInfoDTO.setDeptId(pushOrderInfo.getDeptId());

        Long dateInfoId = specialDataInfoService.saveSpecialDataInfo(taskInfoDTO, userInfoResult, memberExtInfo);
        boolean saveResult = ObjectUtil.isNotEmpty(dateInfoId);
        if (saveResult){
            // 更新用户最后的推送时间
            if (ObjectUtil.isNotEmpty(userInfoResult)){
                specialCostomUserInfoService.userInfoNoticePush(userInfoResult, memberExtInfo);
                specialCostomUserInfoService.updateUserPushTime(userInfoResult.getUserInfoId());
            }
        }

        if (saveResult){
            // 推送结果更新
            specialCostomInfoProfitService.saveSpecialPushProfitInfo(String.valueOf(costomInfo.getId()), pushOrderInfo.getPriceAmount(), PushActionTypeEnum.SUCCESS);
        }else{
            specialCostomInfoProfitService.saveSpecialPushProfitInfo(String.valueOf(costomInfo.getId()), pushOrderInfo.getPriceAmount(), PushActionTypeEnum.REGISTER);
        }

        apiPushResult.setOrderNo(String.valueOf(dateInfoId));
        apiPushResult.setSuccess(saveResult);
        apiPushResult.setSettleFlag(saveResult);
        return apiPushResult;

    }

    /**
     * 推送数据给CRM 机构
     * 撞库以及推送
     */
    @Override
    public ApiPushResult checkAndRegisterUser(SpecialCostomInfo costomInfo, SpecialCostomItemInfo itemInfo, MemberExtInfo memberExtInfo) {

        ApiPushResult apiPushResult = new ApiPushResult();
        apiPushResult.setSuccess(false);

        Long number = specialDataInfoMapper.getEntityByInfoIdAndUid(itemInfo.getInfoId(), memberExtInfo.getUid());
        if (number > 0){
            apiPushResult.setMessage("该数据已经推送过了");
            return apiPushResult;
        }

        CostomUserInfoResult userInfoResult = specialCostomUserInfoService.findOneCanPushUserByCostomId(itemInfo.getInfoId());
        log.info("【机构推送】可推送子账号信息【userInfoResult】{}【itemId】{}【costomId】{}",
                ObjectUtil.isNotEmpty(userInfoResult), itemInfo.getId(), itemInfo.getInfoId());

        TaskInfoDTO taskInfoDTO = new TaskInfoDTO();
        taskInfoDTO.setInfoId(itemInfo.getInfoId());
        taskInfoDTO.setInfoType(itemInfo.getInfoType());
        taskInfoDTO.setInfoName(costomInfo.getName());
        taskInfoDTO.setTaskId(itemInfo.getId());
        taskInfoDTO.setPriceAmount(itemInfo.getPriceAmount());
        taskInfoDTO.setDeptId(itemInfo.getDeptId());

        Long dateInfoId = specialDataInfoService.saveSpecialDataInfo(taskInfoDTO, userInfoResult, memberExtInfo);
        boolean saveResult = ObjectUtil.isNotEmpty(dateInfoId);
        if (saveResult){
            // 更新用户最后的推送时间
            if (ObjectUtil.isNotEmpty(userInfoResult)){
                specialCostomUserInfoService.userInfoNoticePush(userInfoResult, memberExtInfo);
                specialCostomUserInfoService.updateUserPushTime(userInfoResult.getUserInfoId());
            }
        }

        if (saveResult){
            // 推送结果更新
            specialCostomInfoProfitService.saveSpecialPushProfitInfo(String.valueOf(costomInfo.getId()), itemInfo.getPriceAmount(), PushActionTypeEnum.SUCCESS);
        }else{
            specialCostomInfoProfitService.saveSpecialPushProfitInfo(String.valueOf(costomInfo.getId()), itemInfo.getPriceAmount(), PushActionTypeEnum.REGISTER);
        }

        apiPushResult.setOrderNo(String.valueOf(dateInfoId));
        apiPushResult.setSuccess(saveResult);
        apiPushResult.setSettleFlag(saveResult);
        return apiPushResult;
    }

}
